home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / DateFormat.java < prev    next >
Text File  |  1998-09-22  |  23KB  |  622 lines

  1. /*
  2.  * @(#)DateFormat.java    1.25 98/02/02
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32. import java.util.Locale;
  33. import java.util.ResourceBundle;
  34. import java.util.MissingResourceException;
  35. import java.util.TimeZone;
  36. import java.util.Calendar;
  37. import java.util.GregorianCalendar;
  38. import java.util.Date;
  39. import java.text.resources.*;
  40.  
  41. /**
  42.  * DateFormat is an abstract class for date/time formatting subclasses which
  43.  * formats and parses dates or time in a language-independent manner.
  44.  * The date/time formatting subclass, such as SimpleDateFormat, allows for
  45.  * formatting (i.e., date -> text), parsing (text -> date), and
  46.  * normalization.  The date is represented as a <code>Date</code> object or
  47.  * as the milliseconds since January 1, 1970, 00:00:00 GMT.
  48.  *
  49.  * <p>DateFormat provides many class methods for obtaining default date/time
  50.  * formatters based on the default or a given loacle and a number of formatting
  51.  * styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More
  52.  * detail and examples of using these styles are provided in the method
  53.  * descriptions.
  54.  *
  55.  * <p>DateFormat helps you to format and parse dates for any locale.
  56.  * Your code can be completely independent of the locale conventions for
  57.  * months, days of the week, or even the calendar format: lunar vs. solar.
  58.  *
  59.  * <p>To format a date for the current Locale, use one of the
  60.  * static factory methods:
  61.  * <pre>
  62.  *  myString = DateFormat.getDateInstance().format(myDate);
  63.  * </pre>
  64.  * <p>If you are formatting multiple numbers, it is
  65.  * more efficient to get the format and use it multiple times so that
  66.  * the system doesn't have to fetch the information about the local
  67.  * language and country conventions multiple times.
  68.  * <pre>
  69.  *  DateFormat df = DateFormat.getDateInstance();
  70.  *  for (int i = 0; i < a.length; ++i) {
  71.  *    output.println(df.format(myDate[i]) + "; ");
  72.  *  }
  73.  * </pre>
  74.  * <p>To format a number for a different Locale, specify it in the
  75.  * call to getDateInstance().
  76.  * <pre>
  77.  *  DateFormat df = DateFormat.getDateInstance(Locale.FRANCE);
  78.  * </pre>
  79.  * <p>You can use a DateFormat to parse also.
  80.  * <pre>
  81.  *  myDate = df.parse(myString);
  82.  * </pre>
  83.  * <p>Use getDate to get the normal date format for that country.
  84.  * There are other static factory methods available.
  85.  * Use getTime to get the time format for that country.
  86.  * Use getDateTime to get a date and time format. You can pass in different
  87.  * options to these factory methods to control the length of the
  88.  * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends
  89.  * on the locale, but generally:
  90.  * <ul><li>SHORT is completely numeric, such as 12.13.52 or 3:30pm
  91.  * <li>MEDIUM is longer, such as Jan 12, 1952
  92.  * <li>LONG is longer, such as January 12, 1952 or 3:30:32pm
  93.  * <li>FULL is pretty completely specified, such as
  94.  * Tuesday, April 12, 1952 AD or 3:30:42pm PST.
  95.  * </ul>
  96.  *
  97.  * <p>You can also set the time zone on the format if you wish.
  98.  * If you want even more control over the format or parsing,
  99.  * (or want to give your users more control),
  100.  * you can try casting the DateFormat you get from the factory methods
  101.  * to a SimpleDateFormat. This will work for the majority
  102.  * of countries; just remember to put it in a try block in case you
  103.  * encounter an unusual one.
  104.  *
  105.  * <p>You can also use forms of the parse and format methods with
  106.  * ParsePosition and FieldPosition to
  107.  * allow you to
  108.  * <ul><li>pregressively parse through pieces of a string.
  109.  * <li>align any particular field, or find out where it is for selection
  110.  * on the screen.
  111.  * </ul>
  112.  *
  113.  * @see          Format
  114.  * @see          NumberFormat
  115.  * @see          SimpleDateFormat
  116.  * @see          java.util.Calendar
  117.  * @see          java.util.GregorianCalendar
  118.  * @see          java.util.TimeZone
  119.  * @version      1.25 02/02/98
  120.  * @author       Mark Davis, Chen-Lieh Huang, Alan Liu
  121.  */
  122. public abstract class DateFormat extends Format implements java.lang.Cloneable {
  123.  
  124.     /**
  125.      * The calendar that DateFormat uses to produce the time field values
  126.      * needed to implement date/time formatting.  Subclasses should initialize
  127.      * this to the default calendar for the locale associated with this
  128.      * DateFormat.
  129.      */
  130.     protected Calendar calendar;
  131.  
  132.     /**
  133.      * The number formatter that DateFormat uses to format numbers in dates
  134.      * and times.  Subclasses should initialize this to the default number
  135.      * format for the locale associated with this DateFormat.
  136.      */
  137.     protected NumberFormat numberFormat;
  138.  
  139.     /**
  140.      * Useful constant for ERA field alignment.
  141.      * Used in FieldPosition of date/time formatting.
  142.      */
  143.     public final static int ERA_FIELD = 0;
  144.     /**
  145.      * Useful constant for YEAR field alignment.
  146.      * Used in FieldPosition of date/time formatting.
  147.      */
  148.     public final static int YEAR_FIELD = 1;
  149.     /**
  150.      * Useful constant for MONTH field alignment.
  151.      * Used in FieldPosition of date/time formatting.
  152.      */
  153.     public final static int MONTH_FIELD = 2;
  154.     /**
  155.      * Useful constant for DATE field alignment.
  156.      * Used in FieldPosition of date/time formatting.
  157.      */
  158.     public final static int DATE_FIELD = 3;
  159.     /**
  160.      * Useful constant for one-based HOUR_OF_DAY field alignment.
  161.      * Used in FieldPosition of date/time formatting.
  162.      * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
  163.      * For example, 23:59 + 01:00 results in 24:59.
  164.      */
  165.     public final static int HOUR_OF_DAY1_FIELD = 4;
  166.     /**
  167.      * Useful constant for zero-based HOUR_OF_DAY field alignment.
  168.      * Used in FieldPosition of date/time formatting.
  169.      * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
  170.      * For example, 23:59 + 01:00 results in 00:59.
  171.      */
  172.     public final static int HOUR_OF_DAY0_FIELD = 5;
  173.     /**
  174.      * Useful constant for MINUTE field alignment.
  175.      * Used in FieldPosition of date/time formatting.
  176.      */
  177.     public final static int MINUTE_FIELD = 6;
  178.     /**
  179.      * Useful constant for SECOND field alignment.
  180.      * Used in FieldPosition of date/time formatting.
  181.      */
  182.     public final static int SECOND_FIELD = 7;
  183.     /**
  184.      * Useful constant for MILLISECOND field alignment.
  185.      * Used in FieldPosition of date/time formatting.
  186.      */
  187.     public final static int MILLISECOND_FIELD = 8;
  188.     /**
  189.      * Useful constant for DAY_OF_WEEK field alignment.
  190.      * Used in FieldPosition of date/time formatting.
  191.      */
  192.     public final static int DAY_OF_WEEK_FIELD = 9;
  193.     /**
  194.      * Useful constant for DAY_OF_YEAR field alignment.
  195.      * Used in FieldPosition of date/time formatting.
  196.      */
  197.     public final static int DAY_OF_YEAR_FIELD = 10;
  198.     /**
  199.      * Useful constant for DAY_OF_WEEK_IN_MONTH field alignment.
  200.      * Used in FieldPosition of date/time formatting.
  201.      */
  202.     public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
  203.     /**
  204.      * Useful constant for WEEK_OF_YEAR field alignment.
  205.      * Used in FieldPosition of date/time formatting.
  206.      */
  207.     public final static int WEEK_OF_YEAR_FIELD = 12;
  208.     /**
  209.      * Useful constant for WEEK_OF_MONTH field alignment.
  210.      * Used in FieldPosition of date/time formatting.
  211.      */
  212.     public final static int WEEK_OF_MONTH_FIELD = 13;
  213.     /**
  214.      * Useful constant for AM_PM field alignment.
  215.      * Used in FieldPosition of date/time formatting.
  216.      */
  217.     public final static int AM_PM_FIELD = 14;
  218.     /**
  219.      * Useful constant for one-based HOUR field alignment.
  220.      * Used in FieldPosition of date/time formatting.
  221.      * HOUR1_FIELD is used for the one-based 12-hour clock.
  222.      * For example, 11:30 PM + 1 hour results in 12:30 AM.
  223.      */
  224.     public final static int HOUR1_FIELD = 15;
  225.     /**
  226.      * Useful constant for zero-based HOUR field alignment.
  227.      * Used in FieldPosition of date/time formatting.
  228.      * HOUR0_FIELD is used for the zero-based 12-hour clock.
  229.      * For example, 11:30 PM + 1 hour results in 00:30 AM.
  230.      */
  231.     public final static int HOUR0_FIELD = 16;
  232.     /**
  233.      * Useful constant for TIMEZONE field alignment.
  234.      * Used in FieldPosition of date/time formatting.
  235.      */
  236.     public final static int TIMEZONE_FIELD = 17;
  237.  
  238.     // Proclaim serial compatibility with 1.1 FCS
  239.     private static final long serialVersionUID = 7218322306649953788L;
  240.  
  241.     /**
  242.      * Overrides Format.
  243.      * Formats a time object into a time string. Examples of time objects
  244.      * are a time value expressed in milliseconds and a Date object.
  245.      * @param obj must be a Number or a Date.
  246.      * @param toAppendTo the string buffer for the returning time string.
  247.      * @param status the formatting status. On input: an alignment field,
  248.      * if desired. On output: the offsets of the alignment field.
  249.      * @return the formatted time string.
  250.      * @see java.util.Format
  251.      */
  252.     public final StringBuffer format(Object obj, StringBuffer toAppendTo,
  253.                                      FieldPosition fieldPosition)
  254.     {
  255.         if (obj instanceof Number)
  256.             return format( new Date(((Number)obj).longValue()),
  257.                           toAppendTo, fieldPosition );
  258.         else if (obj instanceof Date)
  259.             return format( (Date)obj, toAppendTo, fieldPosition );
  260.         else
  261.             throw new IllegalArgumentException("Cannot format given Object as a Date");
  262.     }
  263.  
  264.     /**
  265.      * Formats a Date into a date/time string.
  266.      * @param date a Date to be formatted into a date/time string.
  267.      * @param toAppendTo the string buffer for the returning date/time string.
  268.      * @param status the formatting status. On input: an alignment field,
  269.      * if desired. On output: the offsets of the alignment field. For
  270.      * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
  271.      * if the given status.field is DateFormat.YEAR_FIELD, the offsets
  272.      * status.beginIndex and status.getEndIndex will be set to 0 and 4,
  273.      * respectively. Notice that if the same time field appears
  274.      * more than once in a pattern, the status will be set for the first
  275.      * occurence of that time field. For instance, formatting a Date to
  276.      * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
  277.      * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
  278.      * the offsets status.beginIndex and status.getEndIndex will be set to
  279.      * 5 and 8, respectively, for the first occurence of the timezone
  280.      * pattern character 'z'.
  281.      * @return the formatted date/time string.
  282.      */
  283.     public abstract StringBuffer format(Date date, StringBuffer toAppendTo,
  284.                                         FieldPosition fieldPosition);
  285.  
  286.     /**
  287.      * Formats a Date into a date/time string.
  288.      * @param date the time value to be formatted into a time string.
  289.      * @return the formatted time string.
  290.      */
  291.     public final String format(Date date)
  292.     {
  293.         return format(date, new StringBuffer(),new FieldPosition(0)).toString();
  294.     }
  295.  
  296.     /**
  297.      * Parse a date/time string.
  298.      *
  299.      * @exception  ParseException  If the given string cannot be parsed as a date.
  300.      *
  301.      * @see #parse(String, ParsePosition)
  302.      */
  303.     public Date parse(String text) throws ParseException
  304.     {
  305.         ParsePosition pos = new ParsePosition(0);
  306.         Date result = parse(text, pos);
  307.         if (pos.index == 0)
  308.             throw new ParseException("Unparseable date: \"" + text + "\"" , 0);
  309.         return result;
  310.     }
  311.  
  312.     /**
  313.      * Parse a date/time string according to the given parse position.  For
  314.      * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
  315.      * that is equivalent to Date(837039928046).
  316.      *
  317.      * <p> By default, parsing is lenient: If the input is not in the form used
  318.      * by this object's format method but can still be parsed as a date, then
  319.      * the parse succeeds.  Clients may insist on strict adherence to the
  320.      * format by calling setLenient(false).
  321.      *
  322.      * @see java.text.DateFormat#setLenient(boolean)
  323.      *
  324.      * @param text  The date/time string to be parsed
  325.      *
  326.      * @param pos   On input, the position at which to start parsing; on
  327.      *              output, the position at which parsing terminated, or the
  328.      *              start position if the parse failed.
  329.      *
  330.      * @return      A Date, or null if the input could not be parsed
  331.      */
  332.     public abstract Date parse(String text, ParsePosition pos);
  333.  
  334.     /**
  335.      * Parse a date/time string into an Object.  This convenience method simply
  336.      * calls parse(String, ParsePosition).
  337.      *
  338.      * @see #parse(String, ParsePosition)
  339.      */
  340.     public Object parseObject (String source, ParsePosition pos)
  341.     {
  342.         return parse(source, pos);
  343.     }
  344.  
  345.     /**
  346.      * Constant for full style pattern.
  347.      */
  348.     public static final int FULL = 0;
  349.     /**
  350.      * Constant for long style pattern.
  351.      */
  352.     public static final int LONG = 1;
  353.     /**
  354.      * Constant for medium style pattern.
  355.      */
  356.     public static final int MEDIUM = 2;
  357.     /**
  358.      * Constant for short style pattern.
  359.      */
  360.     public static final int SHORT = 3;
  361.     /**
  362.      * Constant for default style pattern.
  363.      */
  364.     public static final int DEFAULT = MEDIUM;
  365.  
  366.     /**
  367.      * Gets the time formatter with the default formatting style
  368.      * for the default locale.
  369.      * @return a time formatter.
  370.      */
  371.     public final static DateFormat getTimeInstance()
  372.     {
  373.         return get(DEFAULT, -1, Locale.getDefault());
  374.     }
  375.  
  376.     /**
  377.      * Gets the time formatter with the given formatting style
  378.      * for the default locale.
  379.      * @param style the given formatting style. For example,
  380.      * SHORT for "h:mm a" in the US locale.
  381.      * @return a time formatter.
  382.      */
  383.     public final static DateFormat getTimeInstance(int style)
  384.     {
  385.         return get(style, -1, Locale.getDefault());
  386.     }
  387.  
  388.     /**
  389.      * Gets the time formatter with the given formatting style
  390.      * for the given locale.
  391.      * @param style the given formatting style. For example,
  392.      * SHORT for "h:mm a" in the US locale.
  393.      * @param inLocale the given locale.
  394.      * @return a time formatter.
  395.      */
  396.     public final static DateFormat getTimeInstance(int style,
  397.                                                  Locale aLocale)
  398.     {
  399.         return get(style, -1, aLocale);
  400.     }
  401.  
  402.     /**
  403.      * Gets the date formatter with the default formatting style
  404.      * for the default locale.
  405.      * @return a date formatter.
  406.      */
  407.     public final static DateFormat getDateInstance()
  408.     {
  409.         // +4 to set the correct index for getting data out of
  410.         // LocaleElements.
  411.         return get(-1, DEFAULT + 4, Locale.getDefault());
  412.     }
  413.  
  414.     /**
  415.      * Gets the date formatter with the given formatting style
  416.      * for the default locale.
  417.      * @param style the given formatting style. For example,
  418.      * SHORT for "M/d/yy" in the US locale.
  419.      * @return a date formatter.
  420.      */
  421.     public final static DateFormat getDateInstance(int style)
  422.     {
  423.         return get(-1, style + 4, Locale.getDefault());
  424.     }
  425.  
  426.     /**
  427.      * Gets the date formatter with the given formatting style
  428.      * for the given locale.
  429.      * @param style the given formatting style. For example,
  430.      * SHORT for "M/d/yy" in the US locale.
  431.      * @param inLocale the given locale.
  432.      * @return a date formatter.
  433.      */
  434.     public final static DateFormat getDateInstance(int style,
  435.                                                  Locale aLocale)
  436.     {
  437.         return get(-1, style + 4, aLocale);
  438.     }
  439.  
  440.     /**
  441.      * Gets the date/time formatter with the default formatting style
  442.      * for the default locale.
  443.      * @return a date/time formatter.
  444.      */
  445.     public final static DateFormat getDateTimeInstance()
  446.     {
  447.         return get(DEFAULT, DEFAULT + 4, Locale.getDefault());
  448.     }
  449.  
  450.     /**
  451.      * Gets the date/time formatter with the given date and time
  452.      * formatting styles for the default locale.
  453.      * @param dateStyle the given date formatting style. For example,
  454.      * SHORT for "M/d/yy" in the US locale.
  455.      * @param timeStyle the given time formatting style. For example,
  456.      * SHORT for "h:mm a" in the US locale.
  457.      * @return a date/time formatter.
  458.      */
  459.     public final static DateFormat getDateTimeInstance(int dateStyle,
  460.                                                        int timeStyle)
  461.     {
  462.         return get(timeStyle, dateStyle + 4, Locale.getDefault());
  463.     }
  464.  
  465.     /**
  466.      * Gets the date/time formatter with the given formatting styles
  467.      * for the given locale.
  468.      * @param dateStyle the given date formatting style.
  469.      * @param timeStyle the given time formatting style.
  470.      * @param inLocale the given locale.
  471.      * @return a date/time formatter.
  472.      */
  473.     public final static DateFormat
  474.         getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
  475.     {
  476.         return get(timeStyle, dateStyle + 4, aLocale);
  477.     }
  478.  
  479.     /**
  480.      * Get a default date/time formatter that uses the SHORT style for both the
  481.      * date and the time.
  482.      */
  483.     public final static DateFormat getInstance() {
  484.         return getDateTimeInstance(SHORT, SHORT);
  485.     }
  486.  
  487.     /**
  488.      * Gets the set of locales for which DateFormats are installed.
  489.      * @return the set of locales for which DateFormats are installed.
  490.      */
  491.     public static Locale[] getAvailableLocales()
  492.     {
  493.         return LocaleData.getAvailableLocales("DateTimePatterns");
  494.     }
  495.  
  496.     /**
  497.      * Set the calendar to be used by this date format.  Initially, the default
  498.      * calendar for the specified or default locale is used.
  499.      */
  500.     public void setCalendar(Calendar newCalendar)
  501.     {
  502.         this.calendar = newCalendar;
  503.     }
  504.  
  505.     /**
  506.      * Gets the calendar associated with this date/time formatter.
  507.      * @return the calendar associated with this date/time formatter.
  508.      */
  509.     public Calendar getCalendar()
  510.     {
  511.         return calendar;
  512.     }
  513.  
  514.     /**
  515.      * Allows you to set the number formatter.
  516.      * @param newNumberFormat the given new NumberFormat.
  517.      */
  518.     public void setNumberFormat(NumberFormat newNumberFormat)
  519.     {
  520.         this.numberFormat = newNumberFormat;
  521.     }
  522.  
  523.     /**
  524.      * Gets the number formatter which this date/time formatter uses to
  525.      * format and parse a time.
  526.      * @return the number formatter which this date/time formatter uses.
  527.      */
  528.     public NumberFormat getNumberFormat()
  529.     {
  530.         return numberFormat;
  531.     }
  532.  
  533.     /**
  534.      * Sets the time zone for the calendar of this DateFormat object.
  535.      * @param zone the given new time zone.
  536.      */
  537.     public void setTimeZone(TimeZone zone)
  538.     {
  539.         calendar.setTimeZone(zone);
  540.     }
  541.  
  542.     /**
  543.      * Gets the time zone.
  544.      * @return the time zone associated with the calendar of DateFormat.
  545.      */
  546.     public TimeZone getTimeZone()
  547.     {
  548.         return calendar.getTimeZone();
  549.     }
  550.  
  551.     /**
  552.      * Specify whether or not date/time parsing is to be lenient.  With
  553.      * lenient parsing, the parser may use heuristics to interpret inputs that
  554.      * do not precisely match this object's format.  With strict parsing,
  555.      * inputs must match this object's format.
  556.      * @see java.util.Calendar#setLenient
  557.      */
  558.     public void setLenient(boolean lenient)
  559.     {
  560.         calendar.setLenient(lenient);
  561.     }
  562.  
  563.     /**
  564.      * Tell whether date/time parsing is to be lenient.
  565.      */
  566.     public boolean isLenient()
  567.     {
  568.         return calendar.isLenient();
  569.     }
  570.  
  571.     /**
  572.      * Overrides hashCode
  573.      */
  574.     public int hashCode() {
  575.         return numberFormat.hashCode();
  576.         // just enough fields for a reasonable distribution
  577.     }
  578.  
  579.     /**
  580.      * Overrides equals
  581.      */
  582.     public boolean equals(Object obj) {
  583.         if (this == obj) return true;
  584.         if (obj == null || getClass() != obj.getClass()) return false;
  585.         DateFormat other = (DateFormat) obj;
  586.         return (// calendar.equivalentTo(other.calendar) // THIS API DOESN'T EXIST YET!
  587.                 calendar.getFirstDayOfWeek() == other.calendar.getFirstDayOfWeek() &&
  588.                 calendar.getMinimalDaysInFirstWeek() == other.calendar.getMinimalDaysInFirstWeek() &&
  589.                 calendar.isLenient() == other.calendar.isLenient() &&
  590.                 calendar.getTimeZone().equals(other.calendar.getTimeZone()) &&
  591.                 numberFormat.equals(other.numberFormat));
  592.     }
  593.  
  594.     /**
  595.      * Overrides Cloneable
  596.      */
  597.     public Object clone()
  598.     {
  599.         DateFormat other = (DateFormat) super.clone();
  600.         other.calendar = (Calendar) calendar.clone();
  601.         other.numberFormat = (NumberFormat) numberFormat.clone();
  602.         return other;
  603.     }
  604.  
  605.     private static DateFormat get(int timeStyle, /* -1 for no time */
  606.                                   int dateStyle, /* -1 for no date */
  607.                                   Locale loc) {
  608.         try {
  609.             ResourceBundle resource
  610.                 = ResourceBundle.getBundle
  611.                 ("java.text.resources.LocaleElements", loc);
  612.             return new SimpleDateFormat(timeStyle, dateStyle, loc);
  613.  
  614.         } catch (MissingResourceException e) {
  615.             return new SimpleDateFormat("M/d/yy h:mm a");
  616.         }
  617.     }
  618.  
  619.     protected DateFormat() { }
  620.  
  621. }
  622.